{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1460faf1",
   "metadata": {},
   "source": [
    "-------\n",
    "* 日期：2022-10-12\\week08（周三）\n",
    "* 课程：python\n",
    "* 记录人：刘石珍\n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59d8cb21",
   "metadata": {},
   "source": [
    "# 本周学习内容\n",
    "> 1.有序的数据结构-字符串  \n",
    ">> 1.字符串的基本概念（回顾）  \n",
    ">> 2.字符串的性质和方法   \n",
    ">>> 1.Single quotes:'allows embedded\"double\"quotes'\n",
    ">>> 2.\n",
    "\n",
    "> 2.有序的数据结构-列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2d21d79",
   "metadata": {},
   "source": [
    "# 1.字符串的性质和方法  \n",
    "* 字符串的性质\n",
    ">1.字符串的有序性  \n",
    ">2.字符串是有长度的  \n",
    ">>1.使用len()检查字符串的长度  \n",
    "* 字符串的基础方法 （运算法则）  \n",
    ">1.字符串的取值 :str[index] (values)  \n",
    ">2.字符串的切片：text[start:stop] stop取不到  \n",
    ">3.字符串的拼接： text[] + text[]   \n",
    ">4.in 检查成员是否存在\n",
    "* 字符串的一般方法 （function/Methods 函数/方法） \n",
    ">1.isdigit()  \n",
    ">2.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0018d073",
   "metadata": {},
   "outputs": [],
   "source": [
    "text =\"广州南方学院团委\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d15fbb22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a1fa744b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'州'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "050cd7b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "75d396ea",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [21]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mtext\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m100\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    "text[100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fe3dbbb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "06e6c9ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0] + text[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "43c81785",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0:6] # 等价于text[:6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e9a77386",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[:6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f938ec9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-1:-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2ebf4294",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'院团'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "84869527",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "0d04d2d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院团委广州南方学院团委广州南方学院团委'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 * text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "319f5a11",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (2612864993.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [37]\u001b[1;36m\u001b[0m\n\u001b[1;33m    * in 检查成员对象\u001b[0m\n\u001b[1;37m      ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "* in 检查成员对象  \n",
    "* count(检查字符串出现的次数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b1cba60c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"广州\" in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "7d222728",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"北京\" in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e3e0a833",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 * text.count(\"广州\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6134d86",
   "metadata": {},
   "source": [
    "* isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7aeb13c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "print(19)\n",
    "print(\"19\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "64aa0421",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_1 = 19\n",
    "age_2 = \"19\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "28715735",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19 <class 'int'>\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(age_1,type(age_1))\n",
    "print(age_2,type(age_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c3e3d1cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38\n"
     ]
    }
   ],
   "source": [
    "# 如果字符串是数值，可以用int转换成真正的数值\n",
    "if age_2.isdigit():\n",
    "    print(age_1 + int(age_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f5cc7be",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c08dce1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19 <class 'int'>\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "66565537",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [42]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mage_1\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mage_2\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "age_1 + age_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "a8f9ad35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_2.isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "90ef5c74",
   "metadata": {},
   "outputs": [],
   "source": [
    "name =\"Jack\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "fbb14647",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'Jack'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [47]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'Jack'"
     ]
    }
   ],
   "source": [
    "int(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "cd651579",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38\n"
     ]
    }
   ],
   "source": [
    "if age_2.isdigit():\n",
    "    print(age_1 + int(age_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "58f0ecec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "38"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_1 + int(age_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cfe3577",
   "metadata": {},
   "source": [
    "# 2.有序的数据结构——列表  （List）\n",
    " * 1.列表的概念和基本性质  \n",
    " >1.Python knows a number of compound （混合）data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated（用逗号隔开的） values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.  \n",
    " * 2.列表一般方法 （如何学习） \n",
    " >1.使用.Tab键查看列表的方法  \n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5568bfc",
   "metadata": {},
   "source": [
    "### 2.1列表的性质  \n",
    "* 和字符串的运算法则完全一致  \n",
    "* 注意：字符串是对字符进行操作，列表是对items元素进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "eb1c6040",
   "metadata": {},
   "outputs": [],
   "source": [
    "text =\"37247321820\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "3ebf46ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'7'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "0a84dbdf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表的形式  \n",
    "squares = [1,4,9,16,25] # 列表里面放数值  \n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "1475f5ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "f995ad94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 in squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "87103b44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares.count(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "4e342468",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jacks', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Jacks\",\"Alex\",\"Marry\",\"Jan\"] # 列表里面放字符串\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "56cdd519",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 'Jacks', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares + names "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "3ada658f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2*squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "9d9fcc12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Alex', 'Marry']"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "e2ad2707",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jan']"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "4a98c39e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 18, '杨静雯', 20]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info = [\"Jack\",18,\"杨静雯\",20]\n",
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "e2c9bad5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jacks', 'Marry']"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出”Jack“，”Marry“用切片的方法  \n",
    "names[:3:2]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6dfb4590",
   "metadata": {},
   "source": [
    " * 2.列表一般方法 （如何学习） \n",
    " >1.增加  \n",
    " >2.删除  \n",
    " >3.改  \n",
    " >4.查：in\n",
    " * 列表的学习方法\n",
    " >1.使用.Tab键查看列表的方法  \n",
    " >2.使用help（查看方法的使用规则）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "9916c0a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jacks', 'Alex', 'Marry', 'Jan']"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Jacks\",\"Alex\",\"Marry\",\"Jan\"]\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "c674f227",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "abd5fb32",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"Lihua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "fce16dc0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jacks', 'Alex', 'Marry', 'Jan', 'Lihua', 'Lihua']"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9dfa8710",
   "metadata": {},
   "source": [
    "# 课后作业  \n",
    "* 列表的方法尝试所有的方法  \n",
    "* 总结列表的方法学习心得（表格形式总结）  \n",
    "| 列表的方法 | Describing |使用方法 | 备注 |\n",
    "|--------|-----|------|\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "442ffdf5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8113ea49",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa6dc3f9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
